package nl.moviefan.site.service;

import java.util.List;
import nl.moviefan.site.model.Customer;
import nl.moviefan.site.model.MoviefanUser;
import nl.moviefan.site.repository.CustomerRepository;
import nl.moviefan.site.repository.UserRepository;
import nl.moviefan.site.vestigingen.VestigingConnector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service class for customers
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private UserRepository moviefanUserRepository;
    @Autowired
    private UserService userService;

    public Page<Customer> findAll(Pageable pageable) {
        return customerRepository.findAll(pageable);
    }

    @Transactional
    public Customer save(Customer customer) {
        boolean result = false;
        VestigingConnector vc = new VestigingConnector();
        result = vc.insertCustomer(customer.getVestigingID(), customer.getKlantnummer(),
                customer.getName(), customer.getLastname(), customer.getStreetAddress(),
                customer.getHouseNumber(), customer.getTelephone1(), customer.getCity(),
                customer.getMoviefanUser().getUsername(), customer.getMoviefanUser().getPassword(), null);
        if (result == false) {
            customer = null;
        }
        return customerRepository.save(customer);
    }

    public Customer getCustomerById(Long id) throws Exception {
        if (customerRepository.exists(id)) {
            return customerRepository.findOne(id);
        } else {
            throw new Exception();
        }
    }

    public Customer getCustomerByUsername(String username) {
        MoviefanUser moviefanUser = userService.findByUsername(username);
        if (moviefanUser != null) {
            return customerRepository.findByMoviefanUser(moviefanUser);
        } else {
            return null;
        }
    }

    public Customer insertCustomer(Customer customer) throws RuntimeException {
        MoviefanUser g = customer.getMoviefanUser();
        MoviefanUser gebruikerSaved = userService.save(g);

        if (gebruikerSaved == null) {
            return null;
        }
        customer.setMoviefanUser(gebruikerSaved);
        return customer;
    }

    public int getNieuwKlantnummer() {
        List<Customer> customers = customerRepository.findAll();
        int highestklantnummer = -1;
        int tempklantnummer = -1;
        for (Customer c : customers) {
            tempklantnummer = c.getKlantnummer();
            if (highestklantnummer <= tempklantnummer) {
                highestklantnummer = tempklantnummer;
            }
        }
        highestklantnummer += 1;
        return highestklantnummer;
    }
}
